import socket
import select
import sys
#select.EPOLLIN可读事件，  select.EPOLLOUT可写事件

def tcp_server():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    addr = ('',5888)
    s.bind(addr)
    s.listen(128)
    new_client, arr = s.accept()
    epoll = select.epoll()
    #new_client.fileno()是接收缓冲区，也就是recv
    epoll.register(new_client.fileno(), select.EPOLLIN)
    #sys.stdin.fileno()是标准输入缓冲区，也就是input
    epoll.register(sys.stdin.fileno(), select.EPOLLIN)
    while True:
        events = epoll.poll()
        for fd,event in events:
            if fd ==new_client.fileno():
                data = new_client.recv(100)
                if data:
                    print(data.decode('utf-8'))
                else:
                    print('对方断开了')
                    return
            if fd == sys.stdin.fileno():
                try:
                    tada = input()
                    new_client.send(tada.encode('utf-8'))
                except EOFError:
                    print('退钱')
                    exit()
    new_client.close()
    s.close()

if __name__ == '__main__':
    tcp_server()
